package com.fengze.business.tasks;

import com.fengze.business.domain.RemindInfo;
import com.fengze.business.mapper.PerformancePlanMapper;
import com.fengze.business.mapper.ProjectInfoMapper;
import com.fengze.business.mapper.ProjectPlanMapper;
import com.fengze.business.mapper.RemindInfoMapper;
import com.fengze.business.vo.PerformancePlanVO;
import com.fengze.business.vo.ProjectPlanVO;
import com.fengze.common.utils.DateUtils;
import com.fengze.common.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 提醒任务
 */
@Component("remindTask")
public class RemindTask {


    @Autowired
    private ProjectPlanMapper projectPlanMapper;

    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    @Autowired
    private RemindInfoMapper remindInfoMapper;

    @Autowired
    private PerformancePlanMapper performancePlanMapper;

    /**
     * 处理项目信息预期， 项目计划截止日期未完成，项目超期
     */
    public void dealProjectOverDate(){
        LocalDate today = LocalDate.now();
        // 查询，项目计划，结束日期未完成的项目id
        int status = 0;
        List<Long> projectIdList = projectPlanMapper.queryProjectIdByLastDateAndStatus(today.toString(),status);

        // 设置项目预期
        int projectStatus = 1;
        int projectOldStatus = 0;
        projectInfoMapper.updateProjectStatus(projectIdList,projectStatus,projectOldStatus);
    }




    /**
     *
     * @param aheadDayStr 提前提醒天逗号分隔字符串
     * @param overDayStr  逾期提醒天逗号分隔字符串
     */
    public void warnInfo(String aheadDayStr,String overDayStr,Integer overDay){

        List<RemindInfo> remindList = new ArrayList<>();
        // 提前提醒信息处理
        if (StringUtils.isNotEmpty(aheadDayStr)) {
            String [] dayArr = aheadDayStr.split(",");
            List<Integer> collect = Arrays.stream(dayArr).map(day -> Integer.valueOf(day)).sorted().collect(Collectors.toList());
            for (Integer dayNum : collect) {
                // 提前天数日期
                Date aheadDate = DateUtils.addDays(new Date(), dayNum);
                // 项目计划提醒
                List<ProjectPlanVO> list = projectPlanMapper.queryRemindInfoListByDate(aheadDate);
                for (ProjectPlanVO projectPlanVO : list) {
                    RemindInfo remindInfo = new RemindInfo();
                    remindInfo.setBusiId(projectPlanVO.getId());
                    remindInfo.setCategory("projectPlan");
                    remindInfo.setStatus(0);
                    remindInfo.setCreateTime(new Date());
                    String remindInfoStr = "<<" + projectPlanVO.getProjectName() + ">>[[" + projectPlanVO.getPlanContent() + "]]" ;

                    if(dayNum == 0){
                        remindInfoStr += ",今天到期";
                    }else{
                        remindInfoStr += ",剩余"+ dayNum + "天";
                    }
                    remindInfoStr += ",请及时关注";
                    remindInfo.setRemindInfo(remindInfoStr);
                    remindList.add(remindInfo);
                }
                // 履约计划提醒
                List<PerformancePlanVO> performancePlanList = performancePlanMapper.queryRemindInfoListByDate(aheadDate);
                if(CollectionUtils.isNotEmpty(performancePlanList)){
                    for (PerformancePlanVO performancePlanVO : performancePlanList) {
                        RemindInfo remindInfo = new RemindInfo();
                        remindInfo.setBusiId(performancePlanVO.getId());
                        remindInfo.setCategory("performancePlan");
                        remindInfo.setStatus(0);
                        remindInfo.setCreateTime(new Date());
                        String remindInfoStr = "<<" + performancePlanVO.getContractName() + ">>计划履约" + performancePlanVO.getAmountMoney() + ",实际履约" + performancePlanVO.getActualPerformanceAmount() ;

                        if(dayNum == 0){
                            remindInfoStr += ",今天到期";
                        }else{
                            remindInfoStr += ",剩余"+ dayNum + "天";
                        }
                        remindInfoStr += ",请及时关注";
                        remindInfo.setRemindInfo(remindInfoStr);
                        remindList.add(remindInfo);
                    }



                }
            }
        }

        // 逾期信息提醒处理
        if (StringUtils.isNotEmpty(overDayStr)) {
            String [] dayArr = overDayStr.split(",");
            List<Integer> collect = Arrays.stream(dayArr).map(day -> Integer.valueOf(day)).sorted().collect(Collectors.toList());
            for (Integer dayNum : collect) {
                // 过期日期
                Date overDate = DateUtils.addDays(new Date(), 0-dayNum);

                // 项目计划提醒
                List<ProjectPlanVO> list = projectPlanMapper.queryRemindInfoListByDate(overDate);
                for (ProjectPlanVO projectPlanVO : list) {
                    RemindInfo remindInfo = new RemindInfo();
                    remindInfo.setBusiId(projectPlanVO.getId());
                    remindInfo.setCategory("projectPlan");
                    remindInfo.setStatus(0);
                    remindInfo.setCreateTime(new Date());
                    String remindInfoStr = "<<" + projectPlanVO.getProjectName() + ">>[[" + projectPlanVO.getPlanContent() + "]]" ;

                    if(dayNum == 0){
                        remindInfoStr += ",今天到期";
                    }else{
                        remindInfoStr += ",超期"+ dayNum + "天";
                    }
                    remindInfoStr += ",请及时关注";
                    remindInfo.setRemindInfo(remindInfoStr);
                    remindList.add(remindInfo);
                }
                // 履约计划提醒
                List<PerformancePlanVO> performancePlanList = performancePlanMapper.queryRemindInfoListByDate(overDate);
                if(CollectionUtils.isNotEmpty(performancePlanList)){
                    for (PerformancePlanVO performancePlanVO : performancePlanList) {
                        RemindInfo remindInfo = new RemindInfo();
                        remindInfo.setBusiId(performancePlanVO.getId());
                        remindInfo.setCategory("performancePlan");
                        remindInfo.setStatus(0);
                        remindInfo.setCreateTime(new Date());
                        String remindInfoStr = "<<" + performancePlanVO.getContractName() + ">>计划履约" + performancePlanVO.getAmountMoney() + ",实际履约" + performancePlanVO.getActualPerformanceAmount() ;

                        if(dayNum == 0){
                            remindInfoStr += ",今天到期";
                        }else{
                            remindInfoStr += ",已违约"+ dayNum + "天";
                        }
                        remindInfoStr += ",请及时关注";
                        remindInfo.setRemindInfo(remindInfoStr);
                        remindList.add(remindInfo);
                    }
                }

            }
        }

        if(overDay != null){
            // 过期日期
            Date overDate = DateUtils.addDays(new Date(), 0-overDay);

            // 项目计划提醒
            List<ProjectPlanVO> list = projectPlanMapper.queryRemindInfoListLessDate(overDate);
            for (ProjectPlanVO projectPlanVO : list) {
                RemindInfo remindInfo = new RemindInfo();
                remindInfo.setBusiId(projectPlanVO.getId());
                remindInfo.setCategory("projectPlan");
                remindInfo.setStatus(0);
                remindInfo.setCreateTime(new Date());
                String remindInfoStr = "<<" + projectPlanVO.getProjectName() + ">>[[" + projectPlanVO.getPlanContent() + "]]" ;
                remindInfoStr += ",已经超期"+ overDay + "天以上";

                remindInfoStr += ",请及时关注";
                remindInfo.setRemindInfo(remindInfoStr);
                remindList.add(remindInfo);
            }

            // 履约计划提醒
            List<PerformancePlanVO> performancePlanList = performancePlanMapper.queryRemindInfoListLessDate(overDate);
            if(CollectionUtils.isNotEmpty(performancePlanList)){
                for (PerformancePlanVO performancePlanVO : performancePlanList) {
                    RemindInfo remindInfo = new RemindInfo();
                    remindInfo.setBusiId(performancePlanVO.getId());
                    remindInfo.setCategory("performancePlan");
                    remindInfo.setStatus(0);
                    remindInfo.setCreateTime(new Date());
                    String remindInfoStr = "<<" + performancePlanVO.getContractName() + ">>计划履约" + performancePlanVO.getAmountMoney() + ",实际履约" + performancePlanVO.getActualPerformanceAmount() ;

                    remindInfoStr += ",已违约"+ overDay + "天以上";
                    remindInfoStr += ",请及时关注";
                    remindInfo.setRemindInfo(remindInfoStr);
                    remindList.add(remindInfo);
                }
            }


        }

        // 批量插入报警信息
        if (CollectionUtils.isNotEmpty(remindList)) {
            remindInfoMapper.insertBatch(remindList);
        }

    }

    public static void main(String[] args) {
//        String dayStr = "1,22,33,3,2,4";
//        String [] dayArr = dayStr.split(",");
//        List<Integer> collect = Arrays.stream(dayArr).map(day -> Integer.valueOf(day)).sorted().collect(Collectors.toList());
//        for (Integer i : collect) {
//            System.out.println(i);
//        }

        System.out.println(DateUtils.getNowDate());

        Date aheadDate = DateUtils.addDays(new Date(), 2);
        System.out.println(LocalDate.now());
    }
}
